var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude: '',
    latitude: '',
    markers: [],
    shopinfo: '',
    shoplist: [],
    nearlong: '',
    nearlati: '',
    showmodel:true,
    scale:12,
    mapheight: '40%',
    youtab:2,
    youlist:[],
    now_page:1,
    totalpage:1,
    showmoren:true
  },

  onLoad: function (options) {
  },
  onShow:function(){
    console.log('oil_show');
    this.setData({
      now_page:1
    });
    this.getUserLocation();
  },
  previewImg(e){
    console.log(e)
    let url = e.currentTarget.dataset.url;
    let urls = e.currentTarget.dataset.urls;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls:urls
    })
  },
  getUserLocation: function () {
    console.log('oil_function');
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log('oil_success');
        //获取定位
        that.getaddress(res);
      },
      fail: function () {
        console.log('oil_fail');
        wx.getSetting({
          success: function (res) {
            console.log(res)
            if (res.authSetting["scope.userLocation"] == false) {
              console.log("判断失败后")
              wx.showModal({
                title: '请授权您的地理位置',
                content: '需要您的地理位置',
                success: function (res) {
                  console.log(res.cancel)
                  wx.openSetting({
                    success(res) {
                      if (res.authSetting["scope.userLocation"] == true) {
                        wx.showToast({
                          title: '授权成功',
                          icon: 'success',
                          duration: 1000
                        })
                      } else {
                        wx.showToast({
                          title: '授权失败',
                          icon: 'none',
                          duration: 1000
                        })
                      }
                    },
                    fail: function (res) {
                      console.log("调起失败")
                    }
                  })
                },
                fail: function (res) {
                  console.log(res.cancel)
                }
              })
            } else {
              //用户已授权，但是获取地理位置失败，提示用户去系统设置中打开定位
              wx.showModal({
                title: '',
                content: '请在手机系统设置中打开定位服务',
                showCancel: false,
                confirmText: '确定',
                success: function (res) {
                }
              })
            }
           
          },
        })

      }
    })
  },
  // 获取当前位置
  getaddress: function (res) {
    var that = this;

    // 获取当前地理位置
    var latitude = res.latitude
    var longitude = res.longitude
    that.setData({
      latitude: latitude,
      longitude: longitude
    })
    wx.setStorageSync('latitude', latitude);
    wx.setStorageSync('longitude', longitude);
    that.getnear(longitude, latitude);
    that.getlist(longitude, latitude);
  },
  // 获取最近商铺
  getnear: function (longitude, latitude) {
    var that = this;
    var param = {
      url: "api/shop/shopMap",
      data: {
        type: 3,
        longitude: longitude,
        latitude: latitude
      },
      success: function (e) {
        console.log(e)
        var marker = [];
        var datalist = e.data.data.shop_list;
        if(datalist.length == 0){
            that.setData({
              showmodel:false
            })
        }else{

          for (var i = 0; i < datalist.length; i++) {
            var id = datalist[i].id; //名称
            var lat = datalist[i].latitude; //经度
            var lon = datalist[i].longitude;//纬度
            var info = {
              id: 0,
              iconPath: "/images/jiayouzhan.png",
              latitude: '',
              longitude: '',
            };
            info.id = id,
              info.latitude = lat,
              info.longitude = lon,
              marker.push(info);
          }

          var str = JSON.stringify(marker);
          console.log(marker);
          that.setData({
            markers: marker
          })

        }
        
      },

    };
    app.post(param);
  },

  showstationModal: function (e) {
    var shopid = e.markerId;
    wx.navigateTo({
      url: '/pages/station/stationpay/stationpay?id='+shopid,
    })
  },

  showdaohang: function (e) {
    console.log(e)
    var lati = e.currentTarget.dataset.lati;
    var long = e.currentTarget.dataset.long;
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.openLocation({
          latitude: parseFloat(lati),
          longitude: parseFloat(long),
        })
      }
    })
  },
  // saoma
  saoma: function () {
    var _this = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log(res)
        var path = res.path;
        wx.redirectTo({
          url: '/' + path,
        })
      }
    })
  },

  maptop:function(){
    this.setData({
      showmodel:true,
      mapheight:'40%'
    })
  },

  mapbottom:function(){
    this.setData({
      showmodel: false,
      mapheight:'76%'
    })
  },

  youselect:function(e){
    var index = e.currentTarget.dataset.index;
    console.log(index)
    this.setData({
      youtab: index,
      now_page:1
    })
    this.getlist();
  },

  // 获取列表
  getlist:function(){
    var that = this;
    var param = {
      url:"api/shopr/oilshopMap",
      data:{
        now_page:that.data.now_page,
        oil_type:that.data.youtab,
        longitude:that.data.longitude,
        latitude:that.data.latitude
      },
      success:function(res){
          console.log(res);
          var youlist = res.data.data.shop_list;
          var totalpage = res.data.data.total_page;
          var marker = [];
          if (youlist.length == 0) {
            that.setData({
              showmoren: false
            })
          } else {

            for (var i = 0; i < youlist.length; i++) {
              if (youlist[i].cut1 > 0 || youlist[i].cut1 == 0 ){
                    youlist[i].di1 = true;
              }
              if (youlist[i].cut2 > 0 || youlist[i].cut2 == 0 ){
                    youlist[i].di2 = true;
              }
              youlist[i].cut1 = Math.abs(youlist[i].cut1).toFixed(2);
              youlist[i].cut2 = Math.abs(youlist[i].cut2).toFixed(2);
             
            }

            var str = JSON.stringify(marker);
            that.setData({
              youlist: youlist,
              totalpage:totalpage
            })

        }
      }
    }
    app.post(param);
  },
  //上拉加载
  loadmore:function(){
    var now_page = parseInt(this.data.now_page);
    var totalpage = parseInt(this.data.totalpage);
    if (now_page == totalpage){
      wx.showToast({
        title: '没有更多数据了',
        icon: "none",
        duration: 2000
      })
      return false;
    }else{
      now_page = now_page + 1;
      wx.showLoading({
        title: '玩命加载中',
      })
      var that = this;
      var userid = wx.getStorageSync("userId");
      var param = {
        url: "api/shopr/oilshopMap",
        data: {
          now_page: now_page,
          oil_type: that.data.youtab,
          longitude: that.data.longitude,
          latitude: that.data.latitude
        },
        success: function (res) {
          var youlist = res.data.data.shop_list;
          for (var i = 0; i < youlist.length; i++) {
            if (youlist[i].cut1 > 0 || youlist[i].cut1 == 0) {
              youlist[i].di1 = true;
            }
            if (youlist[i].cut2 > 0 || youlist[i].cut2 == 0 ) {
              youlist[i].di2 = true;
            }
            youlist[i].cut1 = Math.abs(youlist[i].cut1);
            youlist[i].cut2 = Math.abs(youlist[i].cut2);
          }
          that.setData({
            youlist: that.data.youlist.concat(youlist),
            now_page: now_page
          })
          // 隐藏加载框
          wx.hideLoading();

        },
      };
      app.post(param);
    }
   
  }
})